package reservator.common;

import java.io.Serializable;

/**
 * Represent a reservation of the reservator system
 * @author Pascal Cuisinaud, Thomas Di'Meco
 */
public class Reservation implements Serializable
{
    private static final long serialVersionUID = 1L;
    
    private int     id;
    private Client  client;
    private Journey journey;
    
    /**
     * Default constructor
     */
    public Reservation () {}
    
    /**
     * Construct a reservation with the given informations
     * @param id      The reservation id
     * @param client  The reservation client
     * @param journey The reservation journey
     */
    public Reservation (int id, Client client, Journey journey)
    {
        this.id = id;
        this.client = client;
        this.journey = journey;
    }
    
    /**
     * Equals method for reservation
     * @param object to be checked
     * @return true if object equals this Reservation, false otherwise
     */
    @Override
    public boolean equals (Object obj)
    {
        if (this == obj) return true;
        if (!(obj instanceof Reservation)) return false;
        
        Reservation other = (Reservation) obj;
        
        return (this.id == other.id) &&
               this.client.equals(other.client) &&
               this.journey.equals(other.journey);
    }
    
    /**
     * ToString method
     * @return A string that contains the class intance's informations
     */    
    @Override
    public String toString ()
    {
        return "Reservation [id=" + id + ", client=" + client + ", journey="
                + journey + "]";
    }

    /**
     * Create a Reservation from a String which contains reservation
     * informations. (reverse of toString())
     * 
     * @param A
     *            string that contains the class intance's informations
     */
    public void createReservation (String s)
    {
        // extracting data from the string and use setters to put it in the
        // reservation
        String c[] = s.split("client=");
        String j[] = c[1].split("journey=");
        setId(Integer.valueOf(s.split(",")[0].split("=")[1]));
        Client client = new Client();
        Journey journey = new Journey();
        client.createClient(j[0]);
        journey.createJourney(j[1]);
        setClient(client);
        setJourney(journey);
    }

    // ************* Getters and Setters *************
    public int getId ()
    {
        return id;
    }

    public void setId (int id)
    {
        this.id = id;
    }

    public Client getClient ()
    {
        return client;
    }

    public void setClient (Client client)
    {
        this.client = client;
    }

    public Journey getJourney ()
    {
        return journey;
    }

    public void setJourney (Journey journey)
    {
        this.journey = journey;
    }
    // **********************************************
    

}
